package com.pay.merchant.controller;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.pay.constant.Constant;
import com.pay.entity.Merchant;
import com.pay.merchant.mapping.MappingUtils;
import com.pay.service.IBaseInfoService;
import com.pay.service.IMerchantService;

@Controller
public class MerchantController {

	@Autowired
	private IBaseInfoService baseInfoService ; 
	
	@Autowired
	private IMerchantService merchantService ;
	
	@RequestMapping(value=MappingUtils.index)
	public ModelAndView index(Integer loginId) {
		String siteName = baseInfoService.getByKey(Constant.siteName);
		Merchant merchant = merchantService.getById(loginId);
		ModelAndView m = new ModelAndView(MappingUtils.index);
		m.addObject("siteName", siteName) ;
		m.addObject("loginId", loginId) ;
		m.addObject("merchant", merchant) ;
		return m ;
	}
	
	@RequestMapping(value=MappingUtils.welcome)
	public ModelAndView welcome(Integer loginId) {
		ModelAndView m = new ModelAndView(MappingUtils.welcome);
		m.addObject("loginId", loginId) ;
		return m ; 
	}
	
	@RequestMapping(value=MappingUtils.exit)
	public void exit(HttpServletResponse response,HttpServletRequest request,Integer loginId) throws IOException {
		Cookie cookie = new Cookie("token","");
		cookie.setDomain(request.getRemoteHost()) ;
		cookie.setPath("/");
		response.addCookie(cookie);
		
		response.sendRedirect(MappingUtils.login);
	}
}
